﻿using System;
using System.Data;
using System.IO;
using System.Text;
using Voluntariar.Sistema.DAO;


namespace Voluntariar.WEB
{
    public partial class Home : Voluntariar.WEB.Util.PaginaBase
    {

        #region Atributos

        #endregion

        #region Eventos

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    if (Util.Sessao.ObjUsuario != null)
                    {
                        Response.Redirect("/HomeUser.aspx", false);
                    }
                    AtualizaBancodeDados();
                }
            }
            catch (Exception ex)
            {
                TratarErro(ex);
            }
        }


        #endregion

        #region Métodos

        public void AtualizaBancodeDados()
        {

            try
            {
                string pathDBVersion = Server.MapPath(@"~/Banco de Dados/dbVersion.xml");
                System.IO.FileInfo InfoArquivo = new FileInfo(pathDBVersion);
                InfoArquivo.IsReadOnly = false;

                DataSet ds = new DataSet();
                FileStream fsReadXml = new FileStream(pathDBVersion, System.IO.FileMode.Open);
                ds.ReadXml(fsReadXml);

                int versao = VersaoDAO.Pesquisar();
                bool atualiza = false;
                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    if (Convert.ToInt32(item.ItemArray[0]) > (versao))
                    {
                        StreamReader objScript = new StreamReader(Server.MapPath(@"~/Banco de Dados/SQL server/") + item.ItemArray[1].ToString(), Encoding.GetEncoding("iso-8859-1"));
                        string strTemp = string.Empty;
                        while (objScript.Peek() != -1)
                        {
                            strTemp += objScript.ReadLine();
                            strTemp += "\n";
                            if (strTemp.Substring(strTemp.Length - 3, 2).Equals("GO"))//IGUAL
                            {
                                strTemp = strTemp.Remove(strTemp.Length - 3, 2);
                                try
                                {
                                    atualiza = VersaoDAO.ExecuteTXT(strTemp, Convert.ToInt32(item.ItemArray[0]), false);
                                }
                                catch
                                {
                                    objScript.Close();
                                    fsReadXml.Close();
                                    InfoArquivo.IsReadOnly = true;
                                    throw;
                                }

                                strTemp = string.Empty;
                            }
                        }
                        if (atualiza)
                        {
                            VersaoDAO.ExecuteTXT("", Convert.ToInt32(item.ItemArray[0]), true);

                        }
                        else
                            break;
                        objScript.Close();
                        fsReadXml.Close();
                        InfoArquivo.IsReadOnly = true;
                    }
                }
                fsReadXml.Close();
                InfoArquivo.IsReadOnly = true;
                Util.Sessao.Mensagem = string.Empty;
            }
            catch (Exception ex)
            {
                TratarErro(ex);
                Util.Sessao.Mensagem = "Erro ao Atualizar Banco de Dados !!" + ex.Message;
                Response.Redirect("/Login.aspx", false);
            }
        }

        #endregion

    }
}